package com.example.basic.web.service.binding.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.IdUtils;
import com.example.basic.utils.StringUtils;
import com.example.basic.web.enums.AuditEnum;
import com.example.basic.web.enums.YesNoEnum;
import com.example.basic.web.mapper.binding.BindingInfoMapper;
import com.example.basic.web.model.binding.BindingInfo;
import com.example.basic.web.model.notice.InNotice;
import com.example.basic.web.service.binding.BindingInfoService;
import com.example.basic.web.service.device.DeviceService;
import com.example.basic.web.service.notice.InNoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class BindingInfoServiceImpl extends BaseEntityServiceImpl<BindingInfoMapper, BindingInfo> implements BindingInfoService {

    @Resource
    private BindingInfoMapper bindingInfoMapper;

    @Resource
    private InNoticeService inNoticeService;

    @Resource
    private DeviceService deviceService;


    /**
     * 取消绑定
     * @param remainsCode 遗体信息编码
     * @return
     */
    @Override
    public int cancel(String remainsCode) {
        notNull(remainsCode,"遗体信息编码不能为空");

        //绑定信息详情
        LambdaQueryWrapper<BindingInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BindingInfo::getRemainsCode,remainsCode);
        BindingInfo bindingInfo = this.mapper.selectOne(lqw);

        if(bindingInfo == null){
            throw new ServiceException("未获取需要取消的绑定关系");
        }

        if(bindingInfo.getDocumentNo().contains("RN")){
            //获取入库通知单
            InNotice inNotice = inNoticeService.detail(bindingInfo.getDocumentNo());
            if(inNotice != null && AuditEnum.AUDIT.getType().equals(inNotice.getStatus())){
                throw new ServiceException("任务流程已开始，不能取消绑定");
            }
        }else {
            throw new ServiceException("任务流程已开始，不能取消绑定");
        }

        //更新设备状态
        if(StringUtils.notEmptyParams(bindingInfo.getDeviceCode())){
            deviceService.freeOrUse(bindingInfo.getDeviceCode());
        }


        return this.deleteById(bindingInfo.getId());
    }

    /**
     * 新增绑定信息
     * @param bindingInfo
     * @return
     */
    @Override
    public BindingInfo add(BindingInfo bindingInfo) {
        notNull(bindingInfo.getDeviceCode(),"设备编码不能为空");
        notNull(bindingInfo.getRemainsCode(),"遗体编码不能为空");

        this.save(bindingInfo);

        return this.selectById(bindingInfo.getId());
    }

    /**
     * 查询绑定关系
     * @param remainsCode 遗体编码
     * @return
     */
    @Override
    public BindingInfo detail(String remainsCode) {
        notNull(remainsCode, "遗体编码不能为空");

        //绑定信息详情
        LambdaQueryWrapper<BindingInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BindingInfo::getRemainsCode, remainsCode);

        return this.mapper.selectOne(lqw);
    }

    /**
     * 更新绑定关系
     * @param bindingInfo
     * @return
     */
    @Override
    public BindingInfo modify(BindingInfo bindingInfo) {
        notNull(bindingInfo,"绑定关系不能为空");
        notNull(bindingInfo.getId(),"唯一id不能为空");
        notNull(bindingInfo.getRemainsCode(),"遗体信息编码不能为空");

        UpdateWrapper<BindingInfo> updateWrapper = new UpdateWrapper();
        if(StringUtils.emptyParams(bindingInfo.getDeviceCode())){
            updateWrapper.set(BindingInfo.device_code,null);
        }else {
            updateWrapper.set(BindingInfo.device_code,bindingInfo.getDeviceCode());
        }
        if(StringUtils.emptyParams(bindingInfo.getAgvCode())){
            updateWrapper.set(BindingInfo.agv_code,null);
        }else {
            updateWrapper.set(BindingInfo.agv_code,bindingInfo.getAgvCode());
        }
        if(StringUtils.emptyParams(bindingInfo.getDocumentNo())){
            updateWrapper.set(BindingInfo.document_no,null);
        }else {
            updateWrapper.set(BindingInfo.document_no,bindingInfo.getDocumentNo());
        }
        if(StringUtils.emptyParams(bindingInfo.getOriginDeviceCode())){
            updateWrapper.set(BindingInfo.origin_device_code,null);
        }else {
            updateWrapper.set(BindingInfo.origin_device_code,bindingInfo.getOriginDeviceCode());
        }
        updateWrapper.eq(BindingInfo.remains_code,bindingInfo.getRemainsCode());
        this.update(null,updateWrapper);

        return this.detail(bindingInfo.getRemainsCode());
    }

    /**
     * 火化完成
     * @param deviceCode 设备编码
     * @return
     */
    @Override
    public Boolean finishCremation(String deviceCode) {

        //查询绑定信息
        BindingInfo bindingInfo = this.detail(deviceCode);

        if(StringUtils.notEmptyParams(bindingInfo.getDeviceCode())){
            //更新设备占用状态
            deviceService.freeOrUse(bindingInfo.getDeviceCode());
        }

        //删除绑定关系
        this.deleteById(bindingInfo.getId());

        return true;
    }

    /**
     * 批量添加绑定关系
     * @param bindingInfos
     */
    @Override
    public Boolean batchAdd(List<BindingInfo> bindingInfos) {

        bindingInfos.forEach(bindingInfo -> {
            bindingInfo.setId(IdUtils.fastUUID());
        });

        return this.saveBatch(bindingInfos);
    }

    /**
     * 根据设备获取绑定信息
     * @param deviceCode
     * @return
     */
    @Override
    public BindingInfo findByDevice(String deviceCode) {
        LambdaQueryWrapper<BindingInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BindingInfo::getDeviceCode,deviceCode);
        return this.mapper.selectOne(lqw);
    }

}
